package com.example.smartcity.ui.selfCenter.Fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.example.smartcity.R;
import com.example.smartcity.ui.Adapter.OrderArrayAdapter;
import com.example.smartcity.ui.Bean.ItemCode;
import com.example.smartcity.ui.Bean.OrderBean;
import com.example.smartcity.ui.Tools.File;
import com.example.smartcity.ui.Tools.OkHttpUtils;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link PayFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class PayFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private ListView listView;
    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public PayFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment PayFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static PayFragment newInstance(String param1, String param2) {
        PayFragment fragment = new PayFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_pay, container, false);
        listView = view.findViewById(R.id.listView);
        initView();
        return view;
    }
    public void initView(){
        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("selfMessage", Context.MODE_PRIVATE);
        String userID = sharedPreferences.getString("userID",null);
        String newUrl = OkHttpUtils.getBaseUrl(getContext()) + "/userinfo/orders/list?pageNum=1&pageSize=10&userId="+userID;
        OkHttpUtils.getCall(newUrl,new File(getContext()).readFile("Token")).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("TAG", "onFailure: "+e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Gson gson = new Gson();
                ArrayList<ItemCode> arrayList = new ArrayList<>();
                ArrayList<OrderBean.DataBean> tempList = new ArrayList<>();
                String json = response.body().string();
                OrderBean bean = gson.fromJson(json,OrderBean.class);
                if (bean.getCode()==200){
                    List<OrderBean.DataBean> dataBeanList = bean.getData();
                    if (dataBeanList!=null){
                        tempList.addAll(dataBeanList);
                    }
                    for (int i = 0; i < tempList.size(); i++) {
                        ItemCode code = new ItemCode(tempList.get(i).getOrderNum(),tempList.get(i).getPrice()+"",tempList.get(i).getStart()
                        ,tempList.get(i).getEnd(),tempList.get(i).getPath(),tempList.get(i).getUserName(),tempList.get(i).getUserTel());
                        arrayList.add(code);
                    }
                    listView.post(new Runnable() {
                        @Override
                        public void run() {
                            listView.setAdapter(new OrderArrayAdapter(getContext(),R.layout.payitemlayout,arrayList));
                        }
                    });

                }
            }
        });
    }
}